/* Copyright 2011 Elektrobit Wireless Communications Oy (EB)


  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License version 2 as 
  published by the Free Software Foundation.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License along
  with this program; if not, write to the Free Software Foundation, Inc.,
  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.


 *All rights reserved 
 *Version:Elektrobit_Qt_1.0.0_20110305 */


/***********************************************************************
** Based on Photo12
** to replace by Images...
** -------------------------------
** to replace pathView with MouseArea
** -----------------------------------
** to implement different values on: size, moving speed, opacity
   by random.
** ---------------------------------------------------------------
** Change root Item as Image.
** ----------------------------------------
** Implement transform effect of the focused image.
** ----------------------------------------------------
** Add speed up bar, photo counter.
** ---------------------------------------
** Add signal to change xDuration.
** -------------------------------
** Refine speed control bar function.
** ------------------------------------------------
** Add transparency effect when pressing on photos.
** -------------------------------------------------
** Modify photo zoom times, speed bar scale, text scale
** -----------------------------------------------------
** Implement 4 direction drag function of photos.
** ------------------------------------------------
** speed bar text --- 350
************************************************************************/


import Qt 4.7

Image {
    id: photo
    width: 110; height: 110
    smooth: true
    asynchronous: true
    fillMode: Image.PreserveAspectFit

    property real xDuration: 6000
    property real xFrom: -150

    onXChanged: {
        if (x >= 700) {photo.destroy()}
    }

    NumberAnimation on x {
        id: xAnim
        alwaysRunToEnd: false
        from: xFrom; to: 700; duration: photo.xDuration * xDurationChange * ((700 - xFrom) / 850)
    }

    Behavior on x {
        NumberAnimation {id: photoXAnim; duration: 400}
    }

    NumberAnimation on y {id: photoYAnim; running: false; duration: 400}


    Behavior on width {
        NumberAnimation {id: photoWidthAnim; duration: 400}
    }

    Behavior on height {
        NumberAnimation {id: photoHeightAnim; duration: 400}
    }


    Rectangle {
        id: photoBackground
        width: 5 + (photo.sourceSize.width >= photo.sourceSize.height ? photo.width : (photo.sourceSize.width / photo.sourceSize.height) * photo.height)
        height: 5 + (photo.sourceSize.width >= photo.sourceSize.height ? (photo.sourceSize.height / photo.sourceSize.width) * photo.width : photo.height)
        anchors.centerIn: parent
        color: "yellow"
        z: -1
        smooth: true
    }

    MouseArea {
        id: photoMouseArea1
        anchors.fill: photoBackground
        drag.target: parent
        drag.axis: Drag.XandYAxis
        drag.minimumY: 0
        drag.maximumY: 360 - parent.height * photo.scale
        drag.minimumX: 0
        drag.maximumX: 640 - parent.width * photo.scale

        onPressed: {
            xAnim.stop()

            pRMouseArea.enabled = false
            photo.opacity *= 0.5
            photoBackground.color = "skyblue"
        }

        onReleased: {
            xFrom = photo.x
            xAnim.start()

            pRMouseArea.enabled = true
            photo.opacity /= 0.5
            photoBackground.color = "yellow"
        }

        onPositionChanged: {
            xAnim.stop()
        }

        onPressAndHold: {
            xFrom = photo.x

            photoWidthAnim.to = photo.width * 4
            photo.width = 1

            photoHeightAnim.to = photo.height * 4
            photo.height = 1

            photoXAnim.to = 320 - photo.width * 4 / 2
            photo.x = 1

            photoYAnim.to = 180 - photo.height * 4 / 2
            photoYAnim.running = true

            photoMouseArea1.enabled = false
            photoMouseArea2.enabled = true
            photo.z += 5

            pRMouseCover.enabled = true

            layer3OpacityAnim.to = 0.85
            pRLayer3.opacity = 0.01

            photoMouseArea2.drag.minimumX = 320 - photo.width * 4 * photo.scale
            photoMouseArea2.drag.maximumX = 320
            photoMouseArea2.drag.minimumY = 180 - photo.height * 4 * photo.scale
            photoMouseArea2.drag.maximumY = 180

            photo.opacity = 1
            photoBackground.color = "yellow"
        }

    }

    MouseArea {
        id: photoMouseArea2
        anchors.fill: photoBackground
        enabled: false

        drag.target: parent
        drag.axis: Drag.XandYAxis

        onClicked: {
            xAnim.start()

            photoWidthAnim.to = 110
            photo.width = 1

            photoHeightAnim.to = 110
            photo.height = 1

            photoYAnim.running = false

            photoMouseArea1.enabled = true
            photoMouseArea2.enabled = false
            photo.z -= 5

            pRMouseArea.enabled = true

            pRMouseCover.enabled = false
            layer3OpacityAnim.to = 0
            pRLayer3.opacity = 0.01

            photo.y = Math.random() * 250
        }

        onPressAndHold: {
            photoWidthAnim.to = photo.width * 1.5
            photo.width = 1

            photoHeightAnim.to = photo.height *  1.5
            photo.height = 1

            photoXAnim.to = 320 - photo.width * 1.5 / 2
            photo.x = 1

            photoYAnim.to = 180 - photo.height * 1.5 / 2
            photoYAnim.running = true

            photoMouseArea2.drag.minimumX = 320 - photo.width * 1.5 * photo.scale
            photoMouseArea2.drag.maximumX = 320
            photoMouseArea2.drag.minimumY = 180 - photo.height * 1.5 * photo.scale
            photoMouseArea2.drag.maximumY = 180
        }
    }

}

